import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xiaofanshu_flutter/controller/ai_creation_controller.dart';
import 'package:xiaofanshu_flutter/static/custom_color.dart';

/// AI创作页面
class AiCreationPage extends StatefulWidget {
  const AiCreationPage({super.key});

  @override
  State<AiCreationPage> createState() => _AiCreationPageState();
}

class _AiCreationPageState extends State<AiCreationPage> {
  final AiCreationController controller = Get.find();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: _buildAppBar(),
      body: Stack(
        children: [
          // 主内容区域
          SingleChildScrollView(
            padding: const EdgeInsets.only(bottom: 80),
            child: Column(
              children: [
                // 区域A：原文展示区（仅续写模式）
                _buildOriginalContentSection(),
                
                // 区域B：情节输入区
                _buildPlotInputSection(),
                
                // 区域C：快捷风格标签
                _buildStyleOptionsSection(),
                
                // 区域D：生成结果区
                _buildGeneratedContentSection(),
                
                // 区域：反馈优化区（生成后显示）
                _buildFeedbackSection(),
              ],
            ),
          ),
          
          // 区域E：底部操作按钮（固定）
          _buildBottomActionBar(),
        ],
      ),
    );
  }

  /// AppBar
  PreferredSizeWidget _buildAppBar() {
    return AppBar(
      backgroundColor: Colors.white,
      elevation: 0,
      leading: IconButton(
        icon: const Icon(
          Icons.arrow_back_ios,
          color: Colors.black,
          size: 25,
        ),
        onPressed: () {
          Get.back();
        },
      ),
      leadingWidth: 28,
      title: Obx(() => Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
            decoration: BoxDecoration(
              color: controller.isContinuation.value
                  ? CustomColor.primaryColor.withOpacity(0.1)
                  : Colors.orange.withOpacity(0.1),
              borderRadius: BorderRadius.circular(12),
            ),
            child: Text(
              controller.isContinuation.value ? '📝 续写模式' : '✨ 自由创作',
              style: TextStyle(
                fontSize: 12,
                color: controller.isContinuation.value
                    ? CustomColor.primaryColor
                    : Colors.orange,
                fontWeight: FontWeight.w600,
              ),
            ),
          ),
          const SizedBox(width: 8),
          Text(
            controller.isContinuation.value
                ? (controller.creationType.value == 0 ? 'AI续写剧情' : 'AI对话续写')
                : 'AI辅助创作',
            style: const TextStyle(
              color: Colors.black,
              fontSize: 18,
              fontWeight: FontWeight.w600,
            ),
          ),
        ],
      )),
      centerTitle: true,
    );
  }

  /// 区域A：原文展示区（续写模式）
  Widget _buildOriginalContentSection() {
    return Obx(() {
      if (!controller.isContinuation.value) {
        return const SizedBox();
      }

      if (controller.isLoadingOriginal.value) {
        return Container(
          margin: const EdgeInsets.all(16),
          padding: const EdgeInsets.all(20),
          decoration: BoxDecoration(
            color: const Color(0xFFF5F5F5),
            borderRadius: BorderRadius.circular(12),
          ),
          child: const Center(
            child: Column(
              children: [
                CircularProgressIndicator(),
                SizedBox(height: 12),
                Text(
                  '正在加载原文...',
                  style: TextStyle(
                    fontSize: 14,
                    color: CustomColor.unselectedColor,
                  ),
                ),
              ],
            ),
          ),
        );
      }

      return Container(
        margin: const EdgeInsets.all(16),
        decoration: BoxDecoration(
          color: const Color(0xFFF9F9F9),
          borderRadius: BorderRadius.circular(12),
          border: Border.all(
            color: CustomColor.primaryColor.withOpacity(0.2),
            width: 1,
          ),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 标题栏
            GestureDetector(
              onTap: () {
                controller.isOriginalCollapsed.value =
                    !controller.isOriginalCollapsed.value;
              },
              behavior: HitTestBehavior.opaque,
              child: Container(
                padding: const EdgeInsets.all(16),
                decoration: BoxDecoration(
                  color: CustomColor.primaryColor.withOpacity(0.05),
                  borderRadius: const BorderRadius.only(
                    topLeft: Radius.circular(12),
                    topRight: Radius.circular(12),
                  ),
                ),
                child: Row(
                  children: [
                    Container(
                      padding: const EdgeInsets.symmetric(
                        horizontal: 8,
                        vertical: 4,
                      ),
                      decoration: BoxDecoration(
                        color: CustomColor.primaryColor,
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child: const Text(
                        '原文',
                        style: TextStyle(
                          fontSize: 12,
                          color: Colors.white,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    ),
                    const SizedBox(width: 12),
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Obx(() => Text(
                            controller.originalTitle.value,
                            style: const TextStyle(
                              fontSize: 16,
                              fontWeight: FontWeight.w600,
                              color: Colors.black,
                            ),
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                          )),
                          const SizedBox(height: 2),
                          Obx(() => Text(
                            '作者：${controller.originalAuthor.value}',
                            style: TextStyle(
                              fontSize: 12,
                              color: Colors.grey[600],
                            ),
                          )),
                        ],
                      ),
                    ),
                    Obx(() => Icon(
                      controller.isOriginalCollapsed.value
                          ? Icons.keyboard_arrow_down
                          : Icons.keyboard_arrow_up,
                      color: CustomColor.primaryColor,
                    )),
                  ],
                ),
              ),
            ),
            
            // 内容区域
            Obx(() => AnimatedContainer(
              duration: const Duration(milliseconds: 300),
              height: controller.isOriginalCollapsed.value ? 0 : null,
              child: controller.isOriginalCollapsed.value
                  ? const SizedBox()
                  : Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        // 当前节点内容
                        Container(
                          constraints: const BoxConstraints(maxHeight: 300),
                          padding: const EdgeInsets.all(16),
                          child: SingleChildScrollView(
                            child: Obx(() => Text(
                              controller.originalContent.value,
                              style: TextStyle(
                                fontSize: 14,
                                height: 1.6,
                                color: Colors.grey[800],
                              ),
                            )),
                          ),
                        ),
                        
                        // 加载全文按钮
                        Obx(() {
                          if (controller.hasLoadedHistorical.value) {
                            return const SizedBox();
                          }
                          return Container(
                            padding: const EdgeInsets.symmetric(
                              horizontal: 16,
                              vertical: 12,
                            ),
                            child: controller.isLoadingHistorical.value
                                ? Row(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: [
                                      const SizedBox(
                                        width: 16,
                                        height: 16,
                                        child: CircularProgressIndicator(
                                          strokeWidth: 2,
                                          color: CustomColor.primaryColor,
                                        ),
                                      ),
                                      const SizedBox(width: 12),
                                      Text(
                                        '正在加载历史内容...',
                                        style: TextStyle(
                                          fontSize: 14,
                                          color: Colors.grey[600],
                                        ),
                                      ),
                                    ],
                                  )
                                : Center(
                                    child: GestureDetector(
                                      onTap: () {
                                        controller.loadHistoricalContent();
                                      },
                                      child: Container(
                                        padding: const EdgeInsets.symmetric(
                                          horizontal: 20,
                                          vertical: 10,
                                        ),
                                        decoration: BoxDecoration(
                                          gradient: LinearGradient(
                                            colors: [
                                              CustomColor.primaryColor
                                                  .withOpacity(0.1),
                                              CustomColor.primaryColor
                                                  .withOpacity(0.05),
                                            ],
                                          ),
                                          borderRadius:
                                              BorderRadius.circular(20),
                                          border: Border.all(
                                            color: CustomColor.primaryColor
                                                .withOpacity(0.3),
                                            width: 1,
                                          ),
                                        ),
                                        child: const Row(
                                          mainAxisSize: MainAxisSize.min,
                                          children: [
                                            Icon(
                                              Icons.history_edu,
                                              size: 16,
                                              color: CustomColor.primaryColor,
                                            ),
                                            SizedBox(width: 6),
                                            Text(
                                              '加载完整故事链',
                                              style: TextStyle(
                                                fontSize: 14,
                                                color: CustomColor.primaryColor,
                                                fontWeight: FontWeight.w600,
                                              ),
                                            ),
                                          ],
                                        ),
                                      ),
                                    ),
                                  ),
                          );
                        }),
                        
                        // 历史内容展示
                        Obx(() {
                          if (!controller.hasLoadedHistorical.value) {
                            return const SizedBox();
                          }
                          return Container(
                            margin: const EdgeInsets.only(top: 12),
                            padding: const EdgeInsets.all(16),
                            decoration: BoxDecoration(
                              color: Colors.blue.shade50.withOpacity(0.5),
                              border: Border(
                                top: BorderSide(
                                  color: CustomColor.primaryColor
                                      .withOpacity(0.2),
                                  width: 1,
                                ),
                              ),
                            ),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Row(
                                  children: [
                                    Container(
                                      padding: const EdgeInsets.symmetric(
                                        horizontal: 8,
                                        vertical: 4,
                                      ),
                                      decoration: BoxDecoration(
                                        color: CustomColor.primaryColor,
                                        borderRadius: BorderRadius.circular(8),
                                      ),
                                      child: const Text(
                                        '完整故事链',
                                        style: TextStyle(
                                          fontSize: 12,
                                          color: Colors.white,
                                          fontWeight: FontWeight.w600,
                                        ),
                                      ),
                                    ),
                                    const SizedBox(width: 8),
                                    Text(
                                      '从根节点到当前节点',
                                      style: TextStyle(
                                        fontSize: 12,
                                        color: Colors.grey[600],
                                      ),
                                    ),
                                  ],
                                ),
                                const SizedBox(height: 12),
                                Container(
                                  constraints:
                                      const BoxConstraints(maxHeight: 400),
                                  child: SingleChildScrollView(
                                    child: Text(
                                      controller.historicalContent.value,
                                      style: TextStyle(
                                        fontSize: 14,
                                        height: 1.6,
                                        color: Colors.grey[800],
                                      ),
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          );
                        }),
                      ],
                    ),
            )),
          ],
        ),
      );
    });
  }

  /// 区域B：情节输入区
  Widget _buildPlotInputSection() {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              const Icon(
                Icons.edit_note,
                size: 20,
                color: CustomColor.primaryColor,
              ),
              const SizedBox(width: 8),
              const Text(
                '描述你的想法',
                style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.w600,
                  color: Colors.black,
                ),
              ),
              const Spacer(),
              ValueListenableBuilder<TextEditingValue>(
                valueListenable: controller.plotInputController,
                builder: (context, value, child) {
                  int length = value.text.length;
                  return Text(
                    '$length/500',
                    style: TextStyle(
                      fontSize: 12,
                      color: length > 500 ? Colors.red : Colors.grey[500],
                    ),
                  );
                },
              ),
            ],
          ),
          const SizedBox(height: 12),
          Container(
            decoration: BoxDecoration(
              color: const Color(0xFFF9F9F9),
              borderRadius: BorderRadius.circular(12),
              border: Border.all(
                color: Colors.grey[300]!,
                width: 1,
              ),
            ),
            child: TextField(
              controller: controller.plotInputController,
              maxLines: 5,
              maxLength: 500,
              decoration: InputDecoration(
                hintText: controller.isContinuation.value
                    ? '描述你希望故事如何发展...\n例如：主角遇到了神秘人物'
                    : '描述你想创作的内容...\n例如：一个关于友情的温馨故事',
                hintStyle: TextStyle(
                  color: Colors.grey[400],
                  fontSize: 14,
                ),
                border: InputBorder.none,
                contentPadding: const EdgeInsets.all(16),
                counterText: '',
              ),
              style: const TextStyle(
                fontSize: 14,
                height: 1.5,
              ),
            ),
          ),
          const SizedBox(height: 8),
          Text(
            '💡 提示：详细的描述能帮助AI生成更符合你期望的内容',
            style: TextStyle(
              fontSize: 12,
              color: Colors.grey[500],
            ),
          ),
        ],
      ),
    );
  }

  /// 区域C：快捷风格标签
  Widget _buildStyleOptionsSection() {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Row(
            children: [
              Icon(
                Icons.palette_outlined,
                size: 20,
                color: CustomColor.primaryColor,
              ),
              SizedBox(width: 8),
              Text(
                '选择风格',
                style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.w600,
                  color: Colors.black,
                ),
              ),
              SizedBox(width: 8),
              Text(
                '（可多选）',
                style: TextStyle(
                  fontSize: 12,
                  color: CustomColor.unselectedColor,
                ),
              ),
            ],
          ),
          const SizedBox(height: 12),
          Obx(() => Wrap(
            spacing: 8,
            runSpacing: 8,
            children: controller.styleOptions.map((style) {
              bool isSelected = controller.selectedStyles
                  .contains(style['value']);
              return GestureDetector(
                onTap: () {
                  controller.toggleStyle(style['value']);
                },
                child: Container(
                  padding: const EdgeInsets.symmetric(
                    horizontal: 16,
                    vertical: 10,
                  ),
                  decoration: BoxDecoration(
                    color: isSelected
                        ? CustomColor.primaryColor.withOpacity(0.1)
                        : const Color(0xFFF5F5F5),
                    borderRadius: BorderRadius.circular(20),
                    border: Border.all(
                      color: isSelected
                          ? CustomColor.primaryColor
                          : Colors.transparent,
                      width: 1.5,
                    ),
                  ),
                  child: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text(
                        style['icon'],
                        style: const TextStyle(fontSize: 16),
                      ),
                      const SizedBox(width: 6),
                      Text(
                        style['label'],
                        style: TextStyle(
                          fontSize: 14,
                          fontWeight: isSelected
                              ? FontWeight.w600
                              : FontWeight.w400,
                          color: isSelected
                              ? CustomColor.primaryColor
                              : Colors.black,
                        ),
                      ),
                    ],
                  ),
                ),
              );
            }).toList(),
          )),
        ],
      ),
    );
  }

  /// 区域D：生成结果区
  Widget _buildGeneratedContentSection() {
    return Obx(() {
      // 正在生成状态
      if (controller.isGenerating.value) {
        return Container(
          margin: const EdgeInsets.all(16),
          padding: const EdgeInsets.all(32),
          decoration: BoxDecoration(
            gradient: LinearGradient(
              colors: [
                CustomColor.primaryColor.withOpacity(0.05),
                CustomColor.primaryColor.withOpacity(0.02),
              ],
              begin: Alignment.topLeft,
              end: Alignment.bottomRight,
            ),
            borderRadius: BorderRadius.circular(12),
          ),
          child: Column(
            children: [
              // 旋转的AI图标
              TweenAnimationBuilder(
                tween: Tween<double>(begin: 0, end: 1),
                duration: const Duration(seconds: 2),
                builder: (context, double value, child) {
                  return Transform.rotate(
                    angle: value * 6.28,
                    child: Container(
                      width: 60,
                      height: 60,
                      decoration: BoxDecoration(
                        gradient: LinearGradient(
                          colors: [
                            CustomColor.primaryColor.withOpacity(0.6),
                            CustomColor.primaryColor,
                          ],
                        ),
                        borderRadius: BorderRadius.circular(30),
                        boxShadow: [
                          BoxShadow(
                            color: CustomColor.primaryColor.withOpacity(0.3),
                            blurRadius: 15,
                            offset: const Offset(0, 5),
                          ),
                        ],
                      ),
                      child: const Icon(
                        Icons.auto_awesome,
                        color: Colors.white,
                        size: 30,
                      ),
                    ),
                  );
                },
                onEnd: () {
                  // 循环动画
                  if (controller.isGenerating.value) {
                    setState(() {});
                  }
                },
              ),
              const SizedBox(height: 20),
              Obx(() => Text(
                controller.generatingProgress.value,
                style: const TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.w600,
                  color: CustomColor.primaryColor,
                ),
              )),
              const SizedBox(height: 8),
              const LinearProgressIndicator(
                backgroundColor: Color(0xFFE0E0E0),
                valueColor: AlwaysStoppedAnimation<Color>(
                  CustomColor.primaryColor,
                ),
              ),
              const SizedBox(height: 16),
              Text(
                '预计需要 5-10 秒',
                style: TextStyle(
                  fontSize: 12,
                  color: Colors.grey[500],
                ),
              ),
            ],
          ),
        );
      }

      // 未生成状态
      if (!controller.hasGeneratedContent.value) {
        return Container(
          margin: const EdgeInsets.all(16),
          padding: const EdgeInsets.all(32),
          decoration: BoxDecoration(
            color: const Color(0xFFF9F9F9),
            borderRadius: BorderRadius.circular(12),
            border: Border.all(
              color: Colors.grey[300]!,
              width: 1,
              style: BorderStyle.solid,
            ),
          ),
          child: Column(
            children: [
              Icon(
                Icons.lightbulb_outline,
                size: 60,
                color: Colors.grey[400],
              ),
              const SizedBox(height: 16),
              Text(
                '等待生成',
                style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.w600,
                  color: Colors.grey[700],
                ),
              ),
              const SizedBox(height: 8),
              Text(
                '输入你的想法或选择风格，点击"生成"按钮',
                style: TextStyle(
                  fontSize: 14,
                  color: Colors.grey[500],
                ),
                textAlign: TextAlign.center,
              ),
            ],
          ),
        );
      }

      // 已生成状态
      return Container(
        margin: const EdgeInsets.all(16),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(12),
          border: Border.all(
            color: CustomColor.primaryColor.withOpacity(0.3),
            width: 1.5,
          ),
          boxShadow: [
            BoxShadow(
              color: CustomColor.primaryColor.withOpacity(0.1),
              blurRadius: 10,
              offset: const Offset(0, 2),
            ),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 标题栏
            Container(
              padding: const EdgeInsets.all(16),
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  colors: [
                    CustomColor.primaryColor.withOpacity(0.1),
                    CustomColor.primaryColor.withOpacity(0.05),
                  ],
                ),
                borderRadius: const BorderRadius.only(
                  topLeft: Radius.circular(12),
                  topRight: Radius.circular(12),
                ),
              ),
              child: Row(
                children: [
                  const Icon(
                    Icons.auto_awesome,
                    size: 20,
                    color: CustomColor.primaryColor,
                  ),
                  const SizedBox(width: 8),
                  const Text(
                    'AI生成内容',
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w600,
                      color: CustomColor.primaryColor,
                    ),
                  ),
                  const Spacer(),
                  // 历史记录按钮
                  if (controller.generationHistory.length > 1)
                    Container(
                      padding: const EdgeInsets.symmetric(
                        horizontal: 8,
                        vertical: 4,
                      ),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(12),
                      ),
                      child: Row(
                        children: [
                          const Icon(
                            Icons.history,
                            size: 14,
                            color: CustomColor.primaryColor,
                          ),
                          const SizedBox(width: 4),
                          Obx(() => Text(
                            '${controller.generationHistory.length}次',
                            style: const TextStyle(
                              fontSize: 12,
                              color: CustomColor.primaryColor,
                            ),
                          )),
                        ],
                      ),
                    ),
                ],
              ),
            ),
            
            // 生成的内容
            Container(
              constraints: const BoxConstraints(
                minHeight: 200,
                maxHeight: 400,
              ),
              padding: const EdgeInsets.all(16),
              child: SingleChildScrollView(
                child: Obx(() => SelectableText(
                  controller.generatedContent.value,
                  style: const TextStyle(
                    fontSize: 15,
                    height: 1.8,
                    color: Colors.black87,
                  ),
                )),
              ),
            ),
          ],
        ),
      );
    });
  }

  /// 反馈优化区（生成后显示）
  Widget _buildFeedbackSection() {
    return Obx(() {
      if (!controller.hasGeneratedContent.value ||
          controller.isGenerating.value) {
        return const SizedBox();
      }

      return Container(
        margin: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Row(
              children: [
                Icon(
                  Icons.feedback_outlined,
                  size: 20,
                  color: CustomColor.primaryColor,
                ),
                SizedBox(width: 8),
                Text(
                  '优化反馈',
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w600,
                    color: Colors.black,
                  ),
                ),
              ],
            ),
            const SizedBox(height: 12),
            
            // 快捷反馈按钮
            Wrap(
              spacing: 8,
              runSpacing: 8,
              children: [
                _buildQuickFeedbackChip('太短了，再详细一点'),
                _buildQuickFeedbackChip('太长了，精简一些'),
                _buildQuickFeedbackChip('风格不太对'),
                _buildQuickFeedbackChip('换个角度写'),
              ],
            ),
            
            const SizedBox(height: 12),
            
            // 自定义反馈输入
            Container(
              decoration: BoxDecoration(
                color: const Color(0xFFF9F9F9),
                borderRadius: BorderRadius.circular(12),
                border: Border.all(
                  color: Colors.grey[300]!,
                  width: 1,
                ),
              ),
              child: TextField(
                controller: controller.feedbackController,
                maxLines: 3,
                decoration: InputDecoration(
                  hintText: '或者输入自定义反馈...',
                  hintStyle: TextStyle(
                    color: Colors.grey[400],
                    fontSize: 14,
                  ),
                  border: InputBorder.none,
                  contentPadding: const EdgeInsets.all(12),
                  suffixIcon: IconButton(
                    icon: const Icon(
                      Icons.send,
                      color: CustomColor.primaryColor,
                    ),
                    onPressed: () {
                      controller.regenerateWithFeedback();
                    },
                  ),
                ),
                style: const TextStyle(
                  fontSize: 14,
                  height: 1.5,
                ),
              ),
            ),
          ],
        ),
      );
    });
  }

  /// 快捷反馈芯片
  Widget _buildQuickFeedbackChip(String feedback) {
    return GestureDetector(
      onTap: () {
        controller.quickFeedback(feedback);
      },
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
        decoration: BoxDecoration(
          color: Colors.orange.withOpacity(0.1),
          borderRadius: BorderRadius.circular(16),
          border: Border.all(
            color: Colors.orange.withOpacity(0.3),
            width: 1,
          ),
        ),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            const Icon(
              Icons.refresh,
              size: 14,
              color: Colors.orange,
            ),
            const SizedBox(width: 4),
            Text(
              feedback,
              style: const TextStyle(
                fontSize: 13,
                color: Colors.orange,
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// 区域E：底部操作按钮
  Widget _buildBottomActionBar() {
    return Positioned(
      bottom: 0,
      left: 0,
      right: 0,
      child: Container(
        padding: const EdgeInsets.all(16),
        decoration: BoxDecoration(
          color: Colors.white,
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.05),
              blurRadius: 10,
              offset: const Offset(0, -2),
            ),
          ],
        ),
        child: Obx(() => Row(
          children: [
            // 清空按钮
            Expanded(
              flex: 1,
              child: OutlinedButton(
                onPressed: controller.isGenerating.value
                    ? null
                    : () {
                        controller.clearAll();
                      },
                style: OutlinedButton.styleFrom(
                  padding: const EdgeInsets.symmetric(vertical: 14),
                  side: BorderSide(
                    color: controller.isGenerating.value
                        ? Colors.grey[300]!
                        : CustomColor.unselectedColor,
                  ),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(25),
                  ),
                ),
                child: Text(
                  '清空',
                  style: TextStyle(
                    fontSize: 15,
                    color: controller.isGenerating.value
                        ? Colors.grey[400]
                        : CustomColor.unselectedColor,
                  ),
                ),
              ),
            ),
            const SizedBox(width: 12),
            
            // 生成/采用按钮
            Expanded(
              flex: 2,
              child: controller.hasGeneratedContent.value &&
                      !controller.isGenerating.value
                  ? ElevatedButton(
                      onPressed: () {
                        controller.adoptContent();
                      },
                      style: ElevatedButton.styleFrom(
                        padding: const EdgeInsets.symmetric(vertical: 14),
                        backgroundColor: Colors.green,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(25),
                        ),
                        elevation: 0,
                      ),
                      child: const Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Icon(Icons.check_circle, size: 20),
                          SizedBox(width: 6),
                          Text(
                            '采用内容',
                            style: TextStyle(
                              fontSize: 16,
                              fontWeight: FontWeight.w600,
                            ),
                          ),
                        ],
                      ),
                    )
                  : ElevatedButton(
                      onPressed: controller.isGenerating.value
                          ? null
                          : () {
                              controller.generateContent();
                            },
                      style: ElevatedButton.styleFrom(
                        padding: const EdgeInsets.symmetric(vertical: 14),
                        backgroundColor: controller.isGenerating.value
                            ? Colors.grey[400]
                            : CustomColor.primaryColor,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(25),
                        ),
                        elevation: controller.isGenerating.value ? 0 : 2,
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          if (controller.isGenerating.value)
                            const SizedBox(
                              width: 16,
                              height: 16,
                              child: CircularProgressIndicator(
                                strokeWidth: 2,
                                color: Colors.white,
                              ),
                            )
                          else
                            const Icon(Icons.auto_awesome, size: 20),
                          const SizedBox(width: 6),
                          Text(
                            controller.isGenerating.value
                                ? '生成中...'
                                : (controller.hasGeneratedContent.value
                                    ? '重新生成'
                                    : '生成内容'),
                            style: const TextStyle(
                              fontSize: 16,
                              fontWeight: FontWeight.w600,
                            ),
                          ),
                        ],
                      ),
                    ),
            ),
          ],
        )),
      ),
    );
  }
}

